package cn.xyy.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.xyy.security.interceptor.SecurityInterceptor;

/**
 * 
 * @author xyy
 *
 */
@Configuration
public class RegInterceptor implements WebMvcConfigurer {

	/**
	 * 按注册顺序优先级执行
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 登录校验拦截器
		registry.addInterceptor(new SecurityInterceptor());
		// 权限校验拦截器
		registry.addInterceptor(new SaInterceptor());
	}

	/**
	 * 重写 Xyy-Token的注解处理器，增加注解合并功能
	 */
	@Autowired
	public void rewriteSaStrategy() {
		SaStrategy.me.getAnnotation = (element, annotationClass) -> {
			return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass);
		};
	}
}
